/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-04-05 23:47:24
 */
export const setItem = (key, value) => {
  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  window.localStorage.setItem(key, value);
};
export const getItem = key => {
  const data = window.localStorage.getItem(key);
  try {
    return JSON.parse(data);
  } catch (err) {
    return data;
  }
};
export const removeItem = key => {
  window.localStorage.removeItem(key);
};

//防抖函数延迟版
export function debounce(func, delay) {
  let timer;
  return function() {
    let context = this
    let args = arguments
    clearTimeout(timer)
    timer = setTimeout(() => {
      func.apply(context, args)
    }, delay)
  }
}

//排序
export function paixu(array, type) {
  let temp, flag = 1,
    len = array.length
  for (let i = 1; i < len && flag == 1; i++) {
    flag = 0
    for (let j = 0; j < len - i; j++) {
      if (type == 0) { //降序
        if (array[j].lastTime < array[j + 1].lastTime) {
          flag = 1
          temp = array[j]
          array[j] = array[j + 1]
          array[j + 1] = temp
        }
      } else if (type == 1) { //升序
        if (array[j].lastTime > array[j + 1].lastTime) {
          flag = 1
          temp = array[j]
          array[j] = array[j + 1]
          array[j + 1] = temp
        }
      }
    }
  }
  return array
}
